package com.eshop.modules.shop.rest;

import com.eshop.exception.BadRequestException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import com.alibaba.fastjson.JSONObject;
import com.eshop.modules.shop.domain.SystemGroupData;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.SystemGroupDataQueryCriteria;
import com.eshop.modules.shop.service.SystemGroupDataService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:数据配置管理" })
@RestController
@RequestMapping({ "api" })
public class SystemGroupDataController
{
    private final SystemGroupDataService systemGroupDataService;
    
    public SystemGroupDataController(final SystemGroupDataService systemGroupDataService) {
        this.systemGroupDataService = systemGroupDataService;
    }
    
    @Log("查询数据配置")
    @ApiOperation("查询数据配置")
    @GetMapping({ "/yxSystemGroupData" })
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMGROUPDATA_ALL','YXSYSTEMGROUPDATA_SELECT')")
    public ResponseEntity getYxSystemGroupDatas(final SystemGroupDataQueryCriteria criteria, final Pageable pageable) {
        final Sort sort = Sort.by(Sort.Direction.DESC, new String[] { "sort" });
        final Pageable pageableT = (Pageable)PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        return new ResponseEntity(this.systemGroupDataService.queryAll(criteria, pageableT), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("新增数据配置")
    @ApiOperation("新增数据配置")
    @PostMapping({ "/yxSystemGroupData" })
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMGROUPDATA_ALL','YXSYSTEMGROUPDATA_CREATE')")
    public ResponseEntity create(@RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        this.checkParam(jsonObject);
        final SystemGroupData systemGroupData = new SystemGroupData();
        systemGroupData.setGroupName(jsonObject.get("groupName").toString());
        jsonObject.remove("groupName");
        systemGroupData.setValue(jsonObject.toJSONString());
        systemGroupData.setStatus(jsonObject.getInteger("status"));
        systemGroupData.setSort(jsonObject.getInteger("sort"));
        return new ResponseEntity(this.systemGroupDataService.save(systemGroupData), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改数据配置")
    @ApiOperation("修改数据配置")
    @PutMapping({ "/yxSystemGroupData" })
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMGROUPDATA_ALL','YXSYSTEMGROUPDATA_EDIT')")
    public ResponseEntity update(@RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        this.checkParam(jsonObject);
        final SystemGroupData systemGroupData = new SystemGroupData();
        systemGroupData.setGroupName(jsonObject.get("groupName").toString());
        jsonObject.remove("groupName");
        systemGroupData.setValue(jsonObject.toJSONString());
        if (jsonObject.getInteger("status") == null) {
            systemGroupData.setStatus(Integer.valueOf(1));
        }
        else {
            systemGroupData.setStatus(jsonObject.getInteger("status"));
        }
        if (jsonObject.getInteger("sort") == null) {
            systemGroupData.setSort(Integer.valueOf(0));
        }
        else {
            systemGroupData.setSort(jsonObject.getInteger("sort"));
        }
        systemGroupData.setId(Integer.valueOf(jsonObject.get("id").toString()));
        this.systemGroupDataService.saveOrUpdate(systemGroupData);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除数据配置")
    @ApiOperation("删除数据配置")
    @DeleteMapping({ "/yxSystemGroupData/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMGROUPDATA_ALL','YXSYSTEMGROUPDATA_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.systemGroupDataService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
    
    private void checkParam(final JSONObject jsonObject) {
        if (ObjectUtil.isNotNull(jsonObject.get("name")) && StrUtil.isEmpty((CharSequence)jsonObject.getString("name"))) {
            throw new BadRequestException("名称必须填写");
        }
        if (ObjectUtil.isNotNull(jsonObject.get("title")) && StrUtil.isEmpty((CharSequence)jsonObject.getString("title"))) {
            throw new BadRequestException("标题必须填写");
        }
        if (ObjectUtil.isNotNull(jsonObject.get("pic")) && StrUtil.isEmpty((CharSequence)jsonObject.getString("pic"))) {
            throw new BadRequestException("图片必须上传");
        }
        if (ObjectUtil.isNotNull(jsonObject.get("info")) && StrUtil.isEmpty((CharSequence)jsonObject.getString("info"))) {
            throw new BadRequestException("简介必须填写");
        }
    }
}
